﻿using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XiaoYuStock.Core;
using XiaoYuStock.Model;
using XiaoYuStock.Model.Models;
using XiaoYuStockAnalysis.BusinessLogic.Business;

namespace XiaoYuStock.AppService
{
  public  class SynRemotingDataService
    {


        private ShareListDataBusiness shareBus = new ShareListDataBusiness();

        private HistoryDataBusiness historyBus = new HistoryDataBusiness();
        private static string hostUrl = System.Configuration.ConfigurationManager.AppSettings["hostUrl"].ToString();
        private const string encryptKey = "mygrE4TpassPhraZ";
        /// <summary>
        /// 
        /// </summary>
        public void SynSpendingData()
        {
            bool flagStop = false;
            do
            {
                var requestData = new RequestData
                {
                    time = DateTime.Now.Ticks.ToString(),
                    action = "sysdata"

                };
                requestData.nonce = CryptoHelper.GetMd5(requestData.time + encryptKey);
                string result = HttpHelper.OpenRead(hostUrl, JsonConvert.SerializeObject(requestData));

                if (!string.IsNullOrWhiteSpace(result))
                {
                    JObject jo = JObject.Parse(result);

                    if (jo["code"].ToString() == "0")
                    {
                        var retData = JArray.Parse(jo["data"].ToString());
                        if (retData == null || retData.Count < 1)
                        {
                            flagStop = true;

                        }
                        else
                        {
                            foreach (var m in retData)
                            {
                                var historyData = XmlConverter.XmlToObj<HistoryData>(m["Data"].ToString());
                                var ret = historyBus.SynData(historyData);
                                if (ret)
                                {
                                    CallbackRequest(Convert.ToInt32(m["Id"].ToString()));
                                }
                            };
                        }
                    }
                    else
                    {
                        flagStop = true;
                    }

                }
                else
                {
                    flagStop = true;
                }
            } while (flagStop == false);
        }



        private static void CallbackRequest(int dataId)
        {
            var rData = new RequestDataItem
            {
                time = DateTime.Now.Ticks.ToString(),
                action = "sysdatacall",
                id = dataId,

            };
            rData.nonce = CryptoHelper.GetMd5(rData.time + encryptKey);
            var r = HttpHelper.OpenRead(hostUrl, JsonConvert.SerializeObject(rData));
        }

        /// <summary>
        /// 时间戳转为C#格式时间
        /// </summary>
        /// <param name=”timeStamp”></param>
        /// <returns></returns>
        private static DateTime GetTime(string timeStamp)
        {
            DateTime dtStart = TimeZone.CurrentTimeZone.ToLocalTime(new DateTime(1970, 1, 1));
            long lTime = long.Parse(timeStamp + "0000");
            TimeSpan toNow = new TimeSpan(lTime); return dtStart.Add(toNow);
        }
    }
}
